//
//  HttpTool.m
//  测试
//
//  Created by user on 16/5/14.
//  Copyright © 2016年 Burning. All rights reserved.
//

#import "HttpTool.h"
#import "AFHTTPSessionManager.h"

@interface HttpTool()
@property (nonatomic, strong) AFHTTPSessionManager *manager;

@end

@implementation HttpTool

///>服务器地址
static NSString * const AFAppBaseURLString = @"http://api.budejie.com";
/**
 *  manager
 *
 *  @return 返回一个manager
 */
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        ///>设置BaseURl
        _manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:AFAppBaseURLString]] ;
        ///>设置请求头类型
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
        _manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
        NSLog(@"%@",_manager.baseURL);
    }
    return _manager;
}

+ (instancetype)shareInstance
{
    return [[self alloc] init];
}
static HttpTool *_instance;

/**
 *  网络单例
 *
 *  @param zone zone
 *
 *  @return 返回一个单例
 */
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}


+ (void)GET:(NSString *)url parameters:(id)parameters success:(void(^)(id responseObject))success failure:(void (^)(NSError *error))failure
{
    
    [[HttpTool shareInstance] GET:url parameters:parameters success:^(id responseObject) {
        
        if (success) success(responseObject);
        
    } failure:^(NSError *error) {
        
        if (failure) failure(error);
        
    }];
}



+ (void)POST:(NSString *)url parameters:(id)parameters success:(void(^)(id responseObject))success failure:(void (^)(NSError *error))failure
{
    
    [[HttpTool shareInstance] POST:url parameters:parameters success:^(id responseObject) {
        
        if (success) success(responseObject);
        
    } failure:^(NSError *error) {
        
        if (failure) failure(error);
        
    }];
}



- (void)GET:(NSString *)url parameters:(id)parameters success:(void(^)(id responseObject))success failure:(void (^)(NSError *error))failure
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    [self.manager GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
  
        
        [UIApplication sharedApplication].networkActivityIndicatorVisible = false;
        if (success) success(responseObject);
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = false;
        if (failure) failure(error);
    }];
    
}

- (void)POST:(NSString *)url parameters:(id)parameters success:(void(^)(id responseObject))success failure:(void (^)(NSError *error))failure
{
    
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = true;
    
    [self.manager POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = false;
        if (success) success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = false;
        if (failure) failure(error);
    }];
    
}


@end
